import QtQuick 2.0
import "../components"
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.12
Item {

    id: rootSettingsDevice


    property var gEquList: []
    property int gEquCount

    BoxWarning{
        id: boxwarning_delDevice
        box_text: "确认删除吗"
        onAccept: {
            let res = CSetUp.delEquInfo(tempData)
            refresh()
        }
    }

    function refresh(){
        gEquList = CSetUp.queryEquInfo()

        var i = 0;
        gEquCount =  Object.keys(gEquList).length
        console.log("当前设备数量: " + Object.keys(gEquList).length)
        var list = layout_device.children
        for(let key in gEquList){
            list[i].equId =  gEquList[key]["Id"]
            list[i].name =   gEquList[key]["EquName"]
            list[i].type =   gEquList[key]["EquConnectType"]===0 ? 0 : 1
            list[i].route =  gEquList[key]["EquConnectType"]===0 ? gEquList[key]["Ip"] : gEquList[key]["SerialPort"]
            list[i].status = gEquList[key]["EquStatus"]
            list[i].visible = true
            i ++
        }
        for(; i < list.length; i ++){
            list[i].visible = false
        }

    }


    function checkDevice(equId){
        deviceWindow.showExistDevice(equId)
    }
    function deleteDevice(id){

        // 设备状态校验
        var equInfo = CSetUp.querySingleEquInfo(id)
        if(equInfo["EquStatus"].toString()==="2"){
            mainwindow.showToast("无法删除运行中的设备", 1)
            return
        }
        // 删除设备确认
        boxwarning_delDevice.tempData = id
        boxwarning_delDevice.show()

    }
    function connect(id){

        let res = CSetUp.connectEqu(id)
        if(res === 0){
            mainwindow.showToast("连接失败", 1)
        }else if(res === 1){
            mainwindow.showToast("连接成功", 0)
        }

        refresh()
    }


    readonly property real dp: config.dp
    anchors.fill: parent
    anchors.margins: 20*dp


    // 弹窗 ==============================================================================================
    DeviceWindow{
        id: deviceWindow
    }
    BoxDataStatistics{
        id: boxDataStatistics
    }


    Item{
        PushButton{
            height: 24*dp
            width: 68*dp
            radius: 12*dp
            title: "刷新"
            visible: gEquCount !== 0
            button_color: "#F3F3F3"
            text_color: "#1677FF"
            y: 20*dp
            x: 28*dp
            onReleased: {
                layout_device.visible = false
                refresh()
                timer_hide.start()
                CSetUp.printClientList()
            }Timer{
                id: timer_hide
                repeat: false
                interval: 20
                onTriggered: {
                    layout_device.visible = true
                }
            }

        }
        height: 40*dp
        width: parent.width
        PushButton{
            x: parent.width-260*dp
            title: "添加"
            defaultMode1: true
            onReleased:{
                if(gEquCount>=10){
                    mainwindow.showToast("最多添加10台设备", 2)
                }else{
                    deviceWindow.showNewDevice()
                }
            }
        }
        PushButton{
            x: parent.width - 120*dp
            title: "数据统计"
            defaultMode2: true
            onReleased: {
                boxDataStatistics.show()
            }
        }

    }

    ScrollView{
        y: 60*dp
        height: parent.height-60*dp
        width: parent.width
        clip: true
        Grid{

            columns: Math.floor((rootSettingsDevice.width-50*dp) / (290*dp+15*dp))
            columnSpacing: 15*dp
            rowSpacing: 15*dp
            x: 30*dp; y: 5*dp
            id: layout_device

            // 我也不想这么写大一排，但是通过Repeater动态添加的元素，触发不了ScrollView，哪位好心人看到了可以帮我解决一下
            // qml自带的组件咋啥方法都没有……
            DeviceItem{ visible: false }
            DeviceItem{ visible: false }
            DeviceItem{ visible: false }
            DeviceItem{ visible: false }
            DeviceItem{ visible: false }

            DeviceItem{ visible: false }
            DeviceItem{ visible: false }
            DeviceItem{ visible: false }
            DeviceItem{ visible: false }
            DeviceItem{ visible: false }


        }
    }

    Item{
        anchors.fill: parent
        visible: gEquCount === 0
        Image{
            width: 300*dp
            height: 280*dp
            anchors.centerIn: parent
            anchors.verticalCenterOffset: -100*dp
            source: "qrc:/picture/empty.png"
            Text{
                text: "还没有设备，快去添加设备吧~"
                font.pixelSize: config.fontsize + 2
                anchors.horizontalCenter: parent.horizontalCenter
                y: 280*dp
                color: "#AAAAAA"
            }
        }
    }

}
